package cn.itcast.xuesheng.web.Interceptors;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class SenstiveWordsInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String title = request.getParameter("title");
        String context = request.getParameter("context");
        String path = SenstiveWordsInterceptor.class.getClassLoader().getResource("敏感词汇.txt").getPath();
        File file=new File(path);
        InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream(path));
        BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
        String line;
        List<String> list=new ArrayList<>();
        while ((line=bufferedReader.readLine())!=null){
            list.add(line);
        }
        for (String str:list){
            if (title.contains(str)){
                title.replaceAll(str,"****");
            }
            if (context.contains(str)){
                title.replaceAll(str,"****");
            }
        }
        return true;
    }
}
